#include "stdafx.h"
#include "Sprite.h"

using namespace	MomogenkyouHelper;
using namespace	MomogenkyouSystem;

Sprite::Sprite(
MomogenkyouSystem::TextureData *pTexture, 
const	float2	&	vTxPosition, 
const	float2	&	vTxSize,	
const	float2	&	vWidthHeight,
		bool		bXZ)
{
	m_pTextureData	= pTexture;
	m_vTxPosition	= vTxPosition;
	m_vTxSize		= vTxSize;
	m_vWidthHeight	= vWidthHeight;

	m_bXFlip		= m_bYFlip = false;
	m_bXZ			= bXZ;

	PrepareVertices();
}

Sprite::Sprite(
MomogenkyouSystem::TextureData *pTexture, 
const	float	&	fTx, 
const	float	&	fTy, 
const	float	&	fTsx, 
const	float	&	fTsy, 
const	float	&	fWidth, 
const	float	&	fHeight,
		bool		bXZ)
{
	m_pTextureData	= pTexture;
	m_vTxPosition	= float2(fTx, fTy);
	m_vTxSize		= float2(fTsx, fTsy);
	m_vWidthHeight	= float2(fWidth, fHeight);

	m_bXFlip		= m_bYFlip = false;
	m_bXZ			= bXZ;

	PrepareVertices();
}

Sprite::~Sprite()
{
	m_pTextureData = NULL;
}

void	Sprite::PrepareVertices()
{
	m_Vertices[0].TexCoord =  m_vTxPosition;
	m_Vertices[1].TexCoord =  m_vTxPosition;
	m_Vertices[2].TexCoord =  m_vTxPosition;
	m_Vertices[3].TexCoord =  m_vTxPosition;

	m_Vertices[0].TexCoord.x += m_vTxSize.x;
	m_Vertices[1].TexCoord.x +=	m_vTxSize.x;
	m_Vertices[1].TexCoord.y += m_vTxSize.y;
	m_Vertices[2].TexCoord.x += m_vTxSize.x;
	Quad::TransformQuadVertex(m_Vertices, m_vWidthHeight, FLOAT4_ZERO, FLOAT4_ONE, 0, m_bXZ);
}

void	Sprite::SetTexture(MomogenkyouSystem::TextureData	* pTexture)
{
	m_pTextureData = pTexture;

}

void	Sprite::SetTextureRect(const float & fx, const float & fy, const float & fw, const float & fh)
{
	m_vTxPosition	= float2(fx, fy);
	m_vTxSize		= float2(fw, fh);


	m_Vertices[0].TexCoord =  m_vTxPosition;
	m_Vertices[1].TexCoord =  m_vTxPosition;
	m_Vertices[2].TexCoord =  m_vTxPosition;
	m_Vertices[3].TexCoord =  m_vTxPosition;

	m_Vertices[0].TexCoord.x += m_vTxSize.x;
	m_Vertices[1].TexCoord.x +=	m_vTxSize.x;
	m_Vertices[1].TexCoord.y += m_vTxSize.y;
	m_Vertices[2].TexCoord.x += m_vTxSize.x;
}

void	Sprite::SetFlip(bool bX, bool bY)
{
	if(bX != m_bXFlip)
	{
		std::swap(m_Vertices[0].TexCoord.x, m_Vertices[1].TexCoord.x);
		std::swap(m_Vertices[0].TexCoord.y, m_Vertices[1].TexCoord.y);
		std::swap(m_Vertices[3].TexCoord.x, m_Vertices[2].TexCoord.x);
		std::swap(m_Vertices[3].TexCoord.y, m_Vertices[2].TexCoord.y);

		m_bXFlip= !m_bXFlip;
	}

	if(bY != m_bYFlip)
	{
		std::swap(m_Vertices[0].TexCoord.x, m_Vertices[3].TexCoord.x);
		std::swap(m_Vertices[0].TexCoord.y, m_Vertices[3].TexCoord.y);
		std::swap(m_Vertices[1].TexCoord.x, m_Vertices[2].TexCoord.x);
		std::swap(m_Vertices[1].TexCoord.y, m_Vertices[2].TexCoord.y);

		m_bYFlip =! m_bYFlip;
	}
}